<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class TestController extends Controller
{
    /**
     * [index 三像计划开服批量导入].
     *
     * @Author   王文凡
     * @DateTime 2018-04-27
     *
     * @version  1.0
     *
     * @param Request $request [description]
     *
     * @return [type] [description]
     */
    public function index11(Request $request)
    {
        $filePath = $request->file('file')->getRealPath();

        Excel::load($filePath, function ($reader) {
            $data = $reader->get()->toArray();
            $port = 6809;
            foreach ($data[0] as $key => $value) {
                $outtime = strtotime($value['time']);
                $server_id = $value['server_id'];
                $name = $server_id.'.'.$value['server_name'];
                $region = $value['region'];
                if (0 == $server_id % 2) {
                    ++$port;
                    $is_add = 0;
                    $merge = $server_id + 1;
                } else {
                    $is_add = 1;
                    $merge = '';
                }
                $time = time();

                $str = "insert into web_server_plan (`id`,`name`,`region`,`port`,`outtime`,`isadd`,`merge`,`time`,`op`)values({$server_id},'{$name}','{$region}',{$port},{$outtime},{$is_add},'{$merge}',{$time},0);";
                echo $str;
            }
        });
    }

    public function index22(Request $request)
    {
        $filePath = $request->file('file')->getRealPath();

        Excel::load($filePath, function ($reader) {
            $data = $reader->get()->toArray();

            foreach ($data as $key => $value) {
                $outtime = $value['date_time']->timestamp;
                $server_id = $value['server_id'];
                $name = $value['server_name'];
                $region = $value['reg_name'];
                $port = $value['port'];
                $time = time();
                $str = "insert into web_server_plan (`id`,`name`,`region`,`port`,`outtime`,`isadd`,`merge`,`time`,`op`)values({$server_id},'{$name}','{$region}',{$port},{$outtime},0,'',{$time},0);";
                echo $str;
            }
        });
    }

    public function index(Request $request)
    {
        $filePath = $request->file('file')->getRealPath();

        Excel::load($filePath, function ($reader) {
            $data = $reader->get()->toArray();
            dd($data);
            foreach ($data as $key => $value) {
            }
        });
    }
}
